<?php

namespace App\Controllers;

class register extends BaseController
{

    public function index()
    {
        $data['username'] = '';
        $data['errors'] = '';
        if (!$_POST || empty($this->request->getPost())) {
            print_r($this->json_result(false, '提交为空', $data));
            return;
        }

        $this->validation->setRule('username', '登录名', 'trim|strip_tags|required|min_length[3]|max_length[10]');//设置登录名的验证规则
        $this->validation->setRule('password', '登录密码', 'trim|strip_tags|required|min_length[6]|max_length[10]');//设置登录密码的验证规则
        //$this->validation->setRule('pass_confirm', '确认密码', 'trim|strip_tags|required|min_length[6]|max_length[10]|matches[password]');//再次登录密码的验证规则
        $this->validation->setRule('email', '邮箱地址', 'trim|strip_tags|required|valid_email');//邮箱的验证规则
        $this->validation->setRule('telephone', '手机号码','required');
        $this->validation->setRule('email_code', '邮箱验证码','required');

        if (!$this->validation->withRequest($this->request)->run()) {
            $data["errors"] = $this->validation->getErrors();
            print_r($this->json_result(false, '表单验证未通过', $data));
            return;
        }

        $data['username'] =  $this->request->getPost('username');

        $email = $this->request->getPost('email');
        $telephone = $this->request->getPost('telephone');
        $username = $this->request->getPost('username');
        $email_code = $this->request->getPost('email_code');

        if($this->user_model->query_user('email', $email)) {
            print_r($this->json_result(false, '邮箱重复', $data));
            // return;
        }
        if($this->user_model->query_user('username', $username)) {
            print_r($this->json_result(false, '用户名重复', $data));
            return;
        }
        if($this->user_model->query_user('telephone',$telephone)){
            print_r($this->json_result(false, '电话重复', $data));
            return;
        }

        if(!$this->session->has("send_time") || !$this->is_valid($this->session->get("send_time"))) {
            print_r($this->json_result(false, '验证码已经过期', $data));
            return;
        }

        if(!$email_code == $this->session->get("random_code")) {
            print_r($this->json_result(false, '验证码错误', $data));
            return;
        }
        $this->session->remove("send_register_time");
        $this->session->remove("register_code");

        // Encrypt password
        $enc_password = md5($this->request->getPost('password'));
        $user_info = array(
            'username'=> $this->request->getPost('username'),
            'password'=>$enc_password,
            'email'=>$email,
            'telephone'=>$telephone,
            'avatar'=> base_url($this->avatar_path.'temp_avatar.jpg')
        );
        if (!$this->user_model->insert_user($user_info)) {
            print_r($this->json_result(false, '注册失败', $data));
            return;
        }
        print_r ($this->json_result(true, '注册成功', $data));
    }

    public function send_code(){
        $data['errors'] = '';

        if (!$_POST || empty($this->request->getPost())) {
            print_r($this->json_result(false, '提交为空', $data));
            return;
        }

        $this->validation->setRule('email', '邮箱地址', 'trim|strip_tags|required|valid_email');//邮箱的验证规则

        if (!$this->validation->withRequest($this->request)->run()) {
            $data["errors"] = $this->validation->getErrors();
            print_r($this->json_result(false, '表单验证未通过', $data));
            return;
        }

        $email = $this->request->getPost("email");

//        if($this->user_model->query_user('email', $email)) {
//            print_r($this->json_result(false, '邮箱已经注册', $data));
//            return;
//        }

        $mail = new Mail();

        $last_send_time = $this->session->get("send_register_time");
        if($last_send_time && $this->is_valid($last_send_time)) {
            $random_code = $this->session->get("register_code");
        }
        else{
            helper('text');
            $random_code = random_string("numeric",6);
            $this->session->set("register_code",$random_code);
            $this->session->set("send_register_time",time());
        }
        $err = $mail->send_mail($email,"邮箱验证码","本次请求的邮件验证码是:".$random_code."\n本验证码一个小时内有效，请及时输入！");
        if($err === true)
            print_r($this->json_result(true, '发送成功', $data));
        else
        {
            $data['errors'] = $err;
            print_r($this->json_result(false, '发送失败', $data));
        }
    }

    // 是否有效
    private function is_valid($last_send_time)
    {
        return $last_send_time <= time()-3600;
    }

}